<template>
  <div class="addArticleContainer">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加文章</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="back">返回</el-button>
      </div>
      <el-form ref="form" :model="addForm" label-width="140px">
        <el-form-item label="文章类型">
          <el-input v-model="addForm.category" style="width: 300px" />
        </el-form-item>
        <el-form-item label="频道">
          <el-select v-model="addForm.channelId" placeholder="请选择频道">
            <el-option v-for="(item,index) in channels" :key="index" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="文章内容">
          <mavon-editor
            ref="md"
            v-model="addForm.content"
            :subfield="false"
            :autofocus="false"
            style="width: 99%;"
            @imgAdd="$imgAdd"
          />
        </el-form-item>
        <el-form-item label="文章图片">
          <el-upload
            action="/oss/img"
            :file-list="fileList"
            list-type="picture-card"
            name="object"
            :on-success="uploadSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="this.dialogImg" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="文章标题">
          <el-input v-model="addForm.title" style="width: 300px" />
        </el-form-item>
        <el-form-item label="文章状态">
          <el-radio-group v-model="addForm.status">
            <el-radio :label="1">已发布</el-radio>
            <el-radio :label="0">未发布</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">添加</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

import { addArticleList } from '@/api/article'
// eslint-disable-next-line no-unused-vars
import { getChannelAll } from '@/api/channel'
import {uploadFile} from "@/api/file-manger"
export default {
  data() {
    return {
      // 当前预览的url
      dialogImg: '',
      dialogVisible: false,
      // 图片数组
      fileList: [],
      addForm: {},
      images: '',
      channels: []
    }
  },
  created() {
    this.channels = getChannelAll().then(res => {
      console.log('this.channels', res.data)
      this.channels = res.data
    })
  },
  methods: {
    // 点击返回上一个页面
    back() {
      this.$store.dispatch('tagsView/delVisitedView', this.$route)
      this.$router.push('/tweet/article')
    },
    // 提交添加表单数据
    onSubmit() {
      addArticleList({
        category: this.addForm.category,
        channelId: this.addForm.channelId,
        channelName: this.channels[this.channels.findIndex(item => item.id === this.addForm.channelId)].name,
        content: this.addForm.content,
        images: this.fileList.map(item => item.url).join(','),
        status: this.addForm.status,
        title: this.addForm.title
      }).then(() => {
        this.$message.success('添加成功')
        this.back()
      })
    },
    //  图片上传成功时的回调
    uploadSuccess(file, fileList) {
      this.fileList.push({ url: fileList.response.data })
    },
    // 移除上传图片数据
    handleRemove(file, fileList) {
      this.fileList=fileList
    },
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImg = file.url
    },
    // 将图片上传到服务器，返回地址替换到md中
    $imgAdd(pos, $file) {
     console.log("$imgAdd")
      const formData = new FormData()

      formData.append('object', $file)
      uploadFile(formData).then(res => {
        console.log(res.data)
        this.$refs.md.$img2Url(pos, res.data)
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.addArticleContainer {
  padding: 20px;
}
</style>
